package com.example.student.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.student.entity.SysLoginLog;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

/**
 * 登录日志Mapper接口
 */
public interface SysLoginLogMapper extends BaseMapper<SysLoginLog> {

    /**
     * 统计某个时间范围内的登录成功次数
     */
    @Select("SELECT COUNT(*) FROM sys_login_log WHERE status = 1 AND login_time BETWEEN #{startTime} AND #{endTime}")
    int countSuccessfulLogins(@Param("startTime") Date startTime, @Param("endTime") Date endTime);
    
    /**
     * 统计某个时间范围内的登录失败次数
     */
    @Select("SELECT COUNT(*) FROM sys_login_log WHERE status = 0 AND login_time BETWEEN #{startTime} AND #{endTime}")
    int countFailedLogins(@Param("startTime") Date startTime, @Param("endTime") Date endTime);
    
    /**
     * 查询指定用户最近的登录记录
     */
    @Select("SELECT * FROM sys_login_log WHERE user_id = #{userId} ORDER BY login_time DESC LIMIT #{limit}")
    List<SysLoginLog> findRecentLoginsByUserId(@Param("userId") Long userId, @Param("limit") int limit);
} 